package top.malaoshi.webmvc;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 注解过滤器
 */
public class AnnotationFilter {

    /**
     * 判断是否 Controller、RestController、Component、Service、Configuration 注解
     * @return
     */
    public static boolean isComponentAnnotation(Class clazz){
        return clazz.isAnnotationPresent(Controller.class) ||
                clazz.isAnnotationPresent(RestController.class) ||
                clazz.isAnnotationPresent(Component.class) ||
                clazz.isAnnotationPresent(Service.class) ||
                clazz.isAnnotationPresent(Configuration.class);
    }

    /**
     * 是否 ControllerAdvice、RestControllerAdvice 注解修饰类
     * @return
     */
    public static boolean isControllerAdviceAndRestControllerAdvice(Class clazz){
        return clazz.isAnnotationPresent(ControllerAdvice.class) ||
                clazz.isAnnotationPresent(RestControllerAdvice.class) ;
    }

    /**
     * 是否修饰类的注解，具体注解如下：
     * Controller、RestController、Component、Service、Configuration
     * ControllerAdvice、RestControllerAdvice、PropertySource
     * @param clazz
     * @return
     */
    public static boolean isClassAnnotation(Class<?> clazz){
        return clazz.isAnnotationPresent(Controller.class) ||
                clazz.isAnnotationPresent(RestController.class) ||
                clazz.isAnnotationPresent(Component.class) ||
                clazz.isAnnotationPresent(Service.class) ||
                clazz.isAnnotationPresent(Configuration.class) ||
                clazz.isAnnotationPresent(ControllerAdvice.class) ||
                clazz.isAnnotationPresent(RestControllerAdvice.class) ||
                clazz.isAnnotationPresent(PropertySource.class);

    }
    /**
     * 判断是否有修饰类的注解（并且不是注解），具体注解如下：
     * Controller、RestController、Component、Service、Configuration
     * ControllerAdvice、RestControllerAdvice、PropertySource
     * @param clazz
     * @return
     */
    public static boolean hasClassAnnotation(Class<?> clazz){
        return (clazz.isAnnotationPresent(Controller.class) ||
                clazz.isAnnotationPresent(RestController.class) ||
                clazz.isAnnotationPresent(Component.class) ||
                clazz.isAnnotationPresent(Service.class) ||
                clazz.isAnnotationPresent(Configuration.class) ||
                clazz.isAnnotationPresent(ControllerAdvice.class) ||
                clazz.isAnnotationPresent(RestControllerAdvice.class) ||
                clazz.isAnnotationPresent(PropertySource.class)) &&
                !clazz.isAnnotation();

    }
}
